<?php
/**
 * @package App
 * @subpackage resource
 */
 
/**
 *
 * Amazon API リソース
 * 
 * http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=19
 *
 * @package     App
 * @subpackage  resource
 * @author      $Author: moriwaki $
 * @version     $Id: amazon.php 360 2008-10-1 01:58:40Z moriwaki $
 * 
 */
class Amazon extends BEAR_Vo
{
 
    /**
     * API uri
     */
    private static $uri = "http://webservices.amazon.co.jp/onca/xml";

    /**
     * amazon api key
     */
    private static $apiKey = '1SBTPPX5F1T2DE72VM02';
    
    /**
     * amazon アフィリエイトID
     */
    private static $affiliateId = 'mobstyle02-22';
    
    /**
     * 作成
     * 
     * @param array $values
     * + name: string 名前
     * @return integer id or error
     */
    public function onCreate($values)
    {
    }
 
    /**
     * 変更
     * 
     * @param array $values
     * + id: integer $id
     * + name: string 名前
     * @return integer id or error
     */
    public function onUpdate($values)
    {
    }
 
    /**
     * 読み込み
     * 
     * @param array $values
     * + mode: strign $mode
     * @return array
     */
    public function onRead($values)
    {
        //assert & mapping
        self::assert(isset($values['mode']));
        $mode = $values['mode'];
        $option = array(
        "timeout" => "3", // タイムアウトの秒数指定
        );

        $params = array();
        $params['Service'] = 'AWSECommerceService';
        $params['SubscriptionId'] = self::$apiKey;
        $params['AssociateTag'] = self::$affiliateId;

        switch($mode){
        case 'BrowseNodeLookup' :
            $params['Operation'] = 'BrowseNodeLookup';

            $ResponseGroup = App_Values::getValue('ResponseGroup', $values);
            if($ResponseGroup){
                $params['ResponseGroup'] = $ResponseGroup;
            }
            $params['BrowseNodeId'] = App_Values::getValue('BrowseNodeId', $values, 0);
            $p = new App_Url(self::$uri, $params);
            $url = $p->getUrl();
//p($url);
            $req=& new HTTP_Request($url);
            $req->sendRequest();
            $result = $req->getResponseBody();
            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();

            $result = $result['BrowseNodes']['BrowseNode'];

            break;

        case 'ItemSearch' :
            $params['Operation'] = 'ItemSearch';
            $params['ResponseGroup'] = App_Values::getValue('ResponseGroup', $values, 'Medium');
            $params['SearchIndex'] = App_Values::getValue('SearchIndex', $values, 'Books');
            $params['ItemPage'] = App_Values::getValue('page', $values, 1);
            $Keywords = App_Values::getValue('Keywords', $values);
            $BrowseNodeId = App_Values::getValue('BrowseNodeId', $values, 0);
            if ($Keywords){
                $params['Keywords'] = $Keywords;
                if ($BrowseNodeId != 0){
                $params['BrowseNodeId'] = $BrowseNodeId;
                }
            }else{
                $params['BrowseNode'] = App_Values::getValue('BrowseNodeId', $values, 0);
            }
            $params['ContentType'] = 'text/xml';

            $p = new App_Url(self::$uri, $params);
            $url = $p->getUrl();

            $req=& new HTTP_Request($url);
            $req->sendRequest();
            $result = $req->getResponseBody();

            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();
            $result = $result['Items'];

            if (isset($result['Item']['ASIN'])){
                $tmp = $result['Item'];
                $result['Item'] = array();
                $result['Item'][] = $tmp;
            }

            break;

        case 'ItemLookup' :
            $params['Operation'] = 'ItemLookup';
            $params['ResponseGroup'] = App_Values::getValue('ResponseGroup', $values, 'Large');
            $params['ItemId'] = App_Values::getValue('ItemId', $values);
            $params['ContentType'] = 'text/xml';

            $p = new App_Url(self::$uri, $params);
            $url = $p->getUrl();

            $req=& new HTTP_Request($url);
            $req->sendRequest();
            $result = $req->getResponseBody();

            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();

            $result = $result['Items'];

            break;

        case 'SimilarityLookup' :
            $params['Operation'] = 'SimilarityLookup';
            $params['ItemId'] = App_Values::getValue('ItemId', $values);
            $params['ResponseGroup'] = 'Small';
            $params['ContentType'] = 'text/xml';

            $p = new App_Url(self::$uri, $params);
            $url = $p->getUrl();

            $req=& new HTTP_Request($url);
            $req->sendRequest();
            $result = $req->getResponseBody();

            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();

            $result = $result['Items'];

            break;
            
        case 'CartCreate':
            $asin = App_Values::getValue('asin', $values);
            $params['Operation'] = 'CartCreate';
            for ($i=0; $i < count($asin); $i++) { 
                $num = $i + 1;
                $params["Item.{$num}.ASIN"] = $asin[$i];
                $params["Item.{$num}.Quantity"] = 1;
            }
            $p = new App_Url(self::$uri, $params);
            $url = $p->getUrl();

            $req=& new HTTP_Request($url);
            $req->sendRequest();
            $result = $req->getResponseBody();

            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();

            $item = $result['Cart']['CartItems']['CartItem'];
            $result['Cart']['CartItems']['CartItem'] = array();
            for ($i=0; $i < count($item); $i++) { 
                $result['Cart']['CartItems']['CartItem'][$item[$i]['ASIN']] = $item[$i];
            }
            ksort($result['Cart']['CartItems']['CartItem']);
            break;

        case 'CartAdd':
            $asin = App_Values::getValue('asin', $values);
            $params['Operation'] = 'CartAdd';
            $params['CartId'] = App_Values::getValue('CartId', $values);
            $params['HMAC'] = App_Values::getValue('HMAC', $values);
            for ($i=0; $i < count($asin); $i++) { 
                $num = $i + 1;
                $params["Item.{$num}.ASIN"] = $asin[$i];
                $params["Item.{$num}.Quantity"] = 1;
            }
            $p = new App_Url(self::$uri, $params);
            $url = $p->getUrl();

            $req=& new HTTP_Request($url);
            $req->sendRequest();
            $result = $req->getResponseBody();

            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();

            $item = $result['Cart']['CartItems']['CartItem'];
            $result['Cart']['CartItems']['CartItem'] = array();
            for ($i=0; $i < count($item); $i++) { 
                $result['Cart']['CartItems']['CartItem'][$item[$i]['ASIN']] = $item[$i];
            }
            ksort($result['Cart']['CartItems']['CartItem']);
            break;
            
        case 'CartGet':
            $params['Operation'] = 'CartGet';
            $params['CartId'] = App_Values::getValue('CartId', $values);
            $params['HMAC'] = App_Values::getValue('HMAC', $values);

            $p = new App_Url(self::$uri, $params);
            $url = $p->getUrl();

            $req=& new HTTP_Request($url);
            $req->sendRequest();
            $result = $req->getResponseBody();

            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();
            
            $item = $result['Cart']['CartItems']['CartItem'];
            $result['Cart']['CartItems']['CartItem'] = array();
            for ($i=0; $i < count($item); $i++) { 
                $result['Cart']['CartItems']['CartItem'][$item[$i]['ASIN']] = $item[$i];
            }
            ksort($result['Cart']['CartItems']['CartItem']);
            break;
        }
        return $result;
    }
 
    /**
     * 削除
     * 
     * @param array $values
     * + id: integer id
     * @return MDB2_Result
     */
    public function onDelete($values)
    {
    }
}
